package com.tgpms.web.earlierStage.service.impl;

import com.tgpms.common.Result;
import com.tgpms.exception.ExceptionExplain;
import com.tgpms.util.Common;
import com.tgpms.web.earlierStage.dao.UsersAuthorityMapper;
import com.tgpms.web.earlierStage.entity.UsersAuthority;
import com.tgpms.web.earlierStage.service.UsersAuthorityService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @ClassName UsersAuthorityServiceImpl
 * @Description TODO
 * @Author GUAN
 * @Date 2019-12-24 11:28
 * @Version 1.0
 * @return
 **/
@Service
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, rollbackFor = Exception.class)
public class UsersAuthorityServiceImpl implements UsersAuthorityService {

    @Resource
    private UsersAuthorityMapper usersAuthorityMapper;

    @Override
    public Result addUsersAuthority(UsersAuthority usersAuthority, Result result) {
        usersAuthority.setInsertDate(new Date());
        usersAuthority.setRowstamp(Common.fromDateFormats()+ UUID.randomUUID().toString().replaceAll("-", "").toUpperCase());
        UsersAuthority isUsed = usersAuthorityMapper.selectByPrimaryKey(usersAuthority.getObsCode(), usersAuthority.getUsersId());
        int i;
        if (isUsed != null) {
            result.setSuccess(false);
            result.setMsg(ExceptionExplain.ERROR_BY_INSERT.getExplain() + "：用户已新增");
        } else {
            i = usersAuthorityMapper.insertSelective(usersAuthority);
            if(i==1) {
                result.setMsg(ExceptionExplain.SUCCESS_BY_INSERT.getExplain());
                result.setData(usersAuthority.getObsCode());
            } else {
                result.setSuccess(false);
                result.setMsg(ExceptionExplain.ERROR_BY_INSERT.getExplain());
            }
        }
        return result;
    }

    @Override
    public Result addMultiUsersAuthority(UsersAuthority usersAuthority, List<String> obsList, Result result) {
        List<UsersAuthority> usersAuthorityList = new ArrayList<>();
        //将用户现有权限都删掉，再重新增加------20200910
        usersAuthorityMapper.deleteByUserId(usersAuthority.getUsersId());
        int i;
        obsList.forEach(obsInfo -> {
            UsersAuthority temp = new UsersAuthority();
            temp.setInsertDate(new Date());
            temp.setRowstamp(Common.fromDateFormats()+ UUID.randomUUID().toString().replaceAll("-", "").toUpperCase());
            temp.setUsersId(usersAuthority.getUsersId());
            temp.setUserName(usersAuthority.getUserName());
            temp.setAdminYn(usersAuthority.getAdminYn());
            temp.setCheckYn(usersAuthority.getCheckYn());
            temp.setObsCode(obsInfo);
            temp.setInsertUser(usersAuthority.getInsertUser());
            usersAuthorityList.add(temp);
        });
        i = usersAuthorityMapper.batchInsert(usersAuthorityList);
        if (i == 0) {
            result.setSuccess(false);
            result.setMsg(ExceptionExplain.ERROR_BY_INSERT.getExplain());
        } else {
            result.setMsg(ExceptionExplain.SUCCESS_BY_INSERT.getExplain());
        }
        return result;
    }

    @Override
    public Result updateUsersAuthority(UsersAuthority usersAuthority, Result result) {
        usersAuthority.setModifyDate(new Date());
        int i = usersAuthorityMapper.updateByPrimaryKeySelective(usersAuthority);
        if(i==1) {
            result.setMsg(ExceptionExplain.SUCCESS_BY_UPDATE.getExplain());
        }else {
            result.setSuccess(false);
            result.setMsg(ExceptionExplain.ERROR_BY_UPDATE.getExplain());
        }
        return result;
    }

    @Override
    public Result deleteUsersAuthority(String obsCode, String usersId, Result result) {
        int i = usersAuthorityMapper.deleteByPrimaryKey(obsCode, usersId);
        if(i==1) {
            result.setMsg(ExceptionExplain.SUCCESS_BY_DELETE.getExplain());
        }else {
            result.setSuccess(false);
            result.setMsg(ExceptionExplain.ERROR_BY_DELETE.getExplain());
        }
        return result;
    }
}
